package org.winexpress

import grails.converters.JSON
import org.codehaus.groovy.grails.web.json.JSONObject
import org.codehaus.groovy.grails.web.json.JSONArray

class ProductOptionController {
    // shared methods
    def sendResponse(statusCode) {
        if (statusCode == 404) {
            JSONObject o = new JSONObject()
            o.put(statusCode, true)
            render o as JSON
        }
        
        response.status = statusCode
        render response
        
        // 200-OK (e.g. GET success)
        // 201-Created (e.g. POST success)
        // 403-Forbidden (e.g. LOGIN failure)
        // 400-Bad Request (e.g. PUT,POST,DELETE failure)
        // 404-Not Found (e.g. GET failure)
        // 500-Internal Server Error (other...)
    }
    
    

    // REST operations
    // GET
    def show = {
//        println 'ProductOptionController >> Show >> START'
        
        ProductOptionManagement poman = new ProductOptionManagement()
        
        if (params.id && ProductOption.exists(params.id)) {    
            def po = poman.getProductOptionByID(params.id)
            render po as JSON
            sendResponse(200)
            
//            println 'Show Product type by ID >> ' + po.id + " " + po
        }
        else {
            def allPO = poman.listProductOptions()
            if (allPO.length() > 0) {
                render allPO as JSON
                sendResponse(200)
            }
            else {
                sendResponse(404)
            }  
            
//            println 'Listing all Product type >> ' + allPO
        }
        
//        println 'ProductOptionController >> Show >> END'
    }
    
    def showByProduct = {
//        println 'ProductOptionController showByProduct >> Show >> START'
        
        ProductOptionManagement poman = new ProductOptionManagement()
        
        if (params.productId && Product.exists(params.productId)) {    
            def po = poman.listProductOptionsByProductID(params.productId)
            println "specific pID for POs"
            println po.size()
            render po as JSON
            sendResponse(200)
            
//            println 'Show Product option by product ID >> ' + po.id + " " + po
        }
        else {
            println "all POs"
            def allPO = poman.listProductOptions()
            if (allPO.length() > 0) {
                render allPO as JSON
                sendResponse(200)
            }
            else {
                sendResponse(404)
            }  
            
//            println 'Listing all Product type >> ' + allPO
        }
        
//        println 'ProductOptionController showByProduct >> Show >> END'
    }
    
    // PUT
    def update = {
        println 'ProductOptionController >> Update >> START'
        
        ProductOptionManagement poman = new ProductOptionManagement()
           
        JSONObject o = request.JSON
            
        def po = poman.getProductOptionByID(o.get("id"))
        if (o.has("productOptionName")) { po.productOptionName = o.get("productOptionName"); println po.productOptionName }
        if (o.has("description")) { po.description = o.get("description"); println po.description }
        
        def result = poman.updateProductOption(po)
        if (result == true) {
            println 'ProductOptionController >> Update >> ' + result
            sendResponse(200)
        }
        else {
            println 'ProductOptionController >> Update >> ' + result
            sendResponse(400)
        }
        

        println 'ProductOptionController >> Update >> END'

    }
    
    // DELETE
    def delete = {
        println 'ProductOptionController >> delete >> START'
        
        ProductOptionManagement poman = new ProductOptionManagement()
        
        if (params.id && ProductOption.exists(params.id)) {    
            def result = poman.deleteProductOption(id)
            
            if (result == true) {
                println 'ProductOptionController >> delete >> ' + result
                sendResponse(200)
            }
            else {
                println 'ProductOptionController >> delete >> ' + result
                sendResponse(400)
            }
        }
        
        println 'ProductOptionController >> delete >> END'
    }
    
    // POST
    def save = {
        println 'ProductOptionController >> Save >> START'
        
        JSONObject o = request.JSON
        
//        def productOptionName = params['productOptionName']
//        println productOptionName
        
        ProductOptionManagement poman = new ProductOptionManagement()
        
        def result = poman.addProductOption(o)
        if (result == true) {
            println 'ProductOptionController >> Save >> ' + result
            sendResponse(201)
        }
        else {
            println 'ProductOptionController >> Save >> ' + result
            sendResponse(400)
        }
        
        println 'ProductOptionController >> Save >> END'
        redirect(uri:"/ProductOptionCreate")
    }
    
   
}
